var editingId;
$(function () {
    $("#addBtn").click(function () {
        if (editingId != undefined && editingId != -1) {
            //如果处于编辑状态，则先取消对应的编辑项目
            cancel();
        }
        editingId = -1;
        var node = $('#tg').treegrid('getSelected');
        if (node != null) {//在选择的节点下添加行
            $('#tg').treegrid('append', {
                parent: node.id,
                data: [
                    {
                        id: editingId,
                        parent_id: node.id,
                        value: '',
                        code: '',
                        level: parseInt(node.level) + 1,
                        op: 1
                    }
                ]
            });
        } else {//在根节点下添加行
            $('#tg').treegrid('append', {
                parent: 0,
                data: [
                    {

                        id: editingId,
                        value: '',
                        code: '',
                        parent_id: 0,
                        level: 0,
                        op: 1
                    }
                ]
            });
        }

        $('#tg').treegrid('beginEdit', editingId);
        $('#tg').treegrid('select', editingId);
    });

    $("#modifyBtn").click(function () {
        if (editingId != undefined) {
            $('#tg').treegrid('select', editingId);
            return;
        }
        var row = $('#tg').treegrid('getSelected');
        if (row) {
            editingId = row.id;
            $("#tg").treegrid('update', {id: editingId, row: {op: 1}});
            $('#tg').treegrid('beginEdit', editingId);
        }
    });
    $("#generateBtn").click(function () {
        generateLvlTableData(dimId);
    });

    //加载数据
    getTreeToLvlEnumList(dimId);
});

function save() {
    var t = $('#tg');
    t.treegrid('select', editingId);
    t.treegrid('endEdit', editingId);
    var row = $('#tg').treegrid('getSelected');
    var data = {
        id: row.id,
        parent_id: row._parentId,
        value: row.name,
        code: row.nodeCode,
        level: row.level
    };
    var saveUrl = ctx + '/dept/save';
    if (editingId != undefined && editingId != -1) {
        saveUrl = ctx + "/dept/update";
        data.parent_id = row._parentId;
    }
    ajaxRequest(saveUrl, data);
    $('#tg').treegrid("reload");
};
function cancel() {
    if (editingId != undefined && editingId != -1) {
        $("#tg").treegrid('update', {id: editingId, row: {op: 0}});
        $('#tg').treegrid('cancelEdit', editingId);
        $('#tg').treegrid("reload");
        editingId = undefined;
    } else {
        $('#tg').treegrid('remove', -1);
    }
};
function removeMenu() {
    var node = $('#tg').treegrid('getSelected');
    if (node != null) {
        var lvl = node.level;
        if (lvl == '0') {
            alert('维度不能删除!');
            return;
        } else {
            $('#tg').treegrid('remove', node.id);
            ajaxRequest(ctx + "/dept/deleteById", {id: node.id}, deleteResult);
        }
    }
};

function deleteResult(result_data) {
    var result = jQuery.parseJSON( result_data );
    if (result.code==1) {
        alert("删除成功!");
    } else {
        alert("删除失败!");
    }
    editingId = undefined;
    $('#tg').treegrid("unselectAll");
    $('#tg').treegrid("reload");
}

function ajaxRequest(url, data, callBack) {
    $.post(url, data, function (result) {
        if (callBack != undefined) {
            callBack(result);
        } else {
            commonResult(result);
        }
    });
}

function commonResult(result_data) {
    var result = jQuery.parseJSON( result_data );

    if (result.code==1) {
        editingId = undefined;
        //$('#tg').treegrid("unselectAll");
        $('#tg').treegrid("reload");
        alert("处理数据成功!");
    } else {
        editingId = undefined;
        alert("处理数据失败!");
        $('#tg').treegrid("reload");
    }
}

function formatButton(value) {
    var s = "";
    if (value == 1) {
        s = '<a href="javascript:;" onclick="save()">保存</a>|<a href="javascript:;" onclick="cancel()">取消</a>';
    } else {
        s = '<a href="javascript:;" onclick="removeMenu()">删除</a>';
    }
    return s;
}

function getTreeToLvlEnumList(dimId) {
    $("#dimDefTreeEnumLvlTable tbody").html('');
    ajaxRequest(ctx + "/dept/getByDimDefId", {dimId: dimId}, function (data) {
        var html = "";
        for (var index = 0; index < data.length; index++) {
            var listResult = data[index];
            html += "<tr>"
            html += "<td>" + getNotNullString(listResult.enumName1) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue1) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName2) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue2) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName3) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue3) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName4) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue4) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName5) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue5) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName6) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue6) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName7) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue7) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName8) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue8) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName9) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue9) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName10) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue10) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumNameLast) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValueLast) + "</td>";
            html += "</tr>";
        }
        $("#dimDefTreeEnumLvlTable").find("tbody").append(html);
    });
}

//拉平
function generateLvlTableData(nodeId) {
    $("#dimDefTreeEnumLvlTable tbody").html('');
    ajaxRequest(ctx + "/dept/generateLvlTableData", {id: nodeId}, function (data) {
        var html = "";
        for (var index = 0; index < data.length; index++) {
            var listResult = data[index];
            html += "<tr>"
            html += "<td>" + getNotNullString(listResult.enumName1) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue1) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName2) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue2) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName3) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue3) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName4) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue4) + "</td>";

            html += "<td>" + getNotNullString(listResult.enumName5) + "</td>";
            html += "<td>" + getNotNullString(listResult.enumValue5) + "</td>";

            html += "</tr>";
        }
        $("#dimDefTreeEnumLvlTable").find("tbody").append(html);
    });
};

function saveTreeToLvlEnumNew(nodeId) {
    ajaxRequest(ctx + "/dimTreeToLvlEnum/saveTreeToLvl", {id: nodeId}, function (data) {
        if (data == 'true') {
            alert('保存成功!');
            parent.window.closeDimTreeEnumDialog();
        } else {
            alert('保存失败!');
        }
    });
}